const reqInterface = require('../../../../api/models')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseList: null,
    roomList: [],
    info: {
      id: 0,
      time: '',
      house_id: 0,
      room_id: 0,
      water_unit: 0,
      water: 0,
      electricity_unit: 0,
      electricity: 0,
      comprehensive: 0,
      sum_total: 0,
    }
  },

  onShow() {
    this.getHouse()
  },

  submit() {
    let self = this;
    reqInterface.PostFee(self.data.info).then(res => {
      wx.showToast({
        title: '提交成功',
        icon: 'none'
      })
     wx.navigateBack() // 这个是表示返回上一层
    })
  },

  getHouse() {
    let self = this;
    reqInterface.GetHouses({
      page: 1,
      limit: 100,
      name: '',
      id: 0
    }).then(res => {
      self.setData({
        houseList: res
      })
    })
  },

  houseIdValue(e) {
    let self = this;
    self.setData({
      'info.house_id': e.detail.val
    })
    self.getRoom(e.detail.val)
  },

  getRoom(val) {
    let self = this;
    reqInterface.GetRooms({
      page: 1,
      limit: 100,
      house_id: val
    }).then(res => {
      self.setData({
        roomList: res
      })
    })
  },

  roomIdValue(e) {
    let self = this;
    self.setData({
      'info.room_id': e.detail.val
    })
  },

  waterUnitValue(e) {
    let self = this;
    self.setData({
      'info.water_unit': e.detail.val
    })
    self.getSumTotal()
  },

  waterValue(e) {
    let self = this;
    self.setData({
      'info.water': e.detail.val
    })
    self.getSumTotal()

  },


  electricityUnitValue(e) {
    let self = this;
    self.setData({
      'info.electricity_unit': e.detail.val
    })
    self.getSumTotal()

  },

  electricityValue(e) {
    let self = this;
    self.setData({
      'info.electricity': e.detail.val
    })
    self.getSumTotal()

  },

  comprehensiveValue(e) {
    let self = this;
    self.setData({
      'info.comprehensive': e.detail.val
    })
    self.getSumTotal()

  },

  getSumTotal() {
    let self = this;
    self.data.info.sum_total = Number(self.data.info.water_unit * self.data.info.water) + Number(self.data.info.electricity_unit * self.data.info.electricity) + Number(self.data.info.comprehensive);
    self.setData({
      info: self.data.info
    })
  }
})